package com.hlingsoft.cms.action.manage;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import com.hlingsoft.cms.action.BaseAction;
import com.hlingsoft.cms.constant.TagsConstant;
import com.hlingsoft.cms.dao.TagsDao;
import com.hlingsoft.cms.entity.Goods;
import com.hlingsoft.cms.entity.Image;
import com.hlingsoft.cms.entity.Tags;
import com.hlingsoft.cms.entity.vo.JsonVo;
import com.hlingsoft.cms.entity.vo.PageVo;
import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* Created by selbylei on 2015/6/2.
*/

@Controller
@RequestMapping("/manage/tags")
public class ManageTagsAction extends ManageBaseAction {

    @Autowired
    private TagsDao tagsDao;

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String index(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<Tags> pageVo = new PageVo<Tags>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = "/manage/goods/list.htm";

        List<Tags> tagsList = tagsDao.getTags2(1,pageVo.getOffset(),pageVo.getRows());
        pageVo.setList(tagsList);
        pageVo.setCount(tagsDao.getTags2Count(1));

        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);

        return "manage/tags/list";
    }


    /**
     * 贴纸添加页面
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.htm", method = RequestMethod.GET)
    public String add(
            ModelMap modelMap) {
        return "manage/tags/add";
    }



    @ResponseBody
    @RequestMapping(value = "/add.json")
    public JsonVo<String> add(
            @RequestParam(value = "type") int type,
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {

            Date now = new Date();
            Tags tags = new Tags();
            tags.setType(type);
            tags.setAppUserId(0);
            tags.setCreateTime(now);
            tags.setUpdateTime(now);
            if (file!=null) {
                String fileName = file.getOriginalFilename();
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = simpleFormat.format(new Date());
                putObject(dateString, fileName, file.getInputStream(),
                        file.getSize());
                String path = "http://icake.oss-cn-hangzhou.aliyuncs.com/tags/" + dateString + "/" + fileName;
                tags.setPath(path);
            }
            tagsDao.addTags(tags);
            json.setResult(true);
            return json;
        } catch (IOException e) {
            e.printStackTrace();
            json.setResult(false);
            return json;
        }
    }

    /**
     * 删除
     * @param tagsId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "tagsId") long tagsId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        tagsDao.deleteTags(tagsId);
        json.setResult(true);
        return json;
    }



    public static void putObject(String date, String filename, InputStream content,
                                 Long size) throws FileNotFoundException {

        // 初始化OSSClient
        OSSClient client = new OSSClient("W6PVM427mSEWlvEz",
                "h7NoXlxcjCqyeOWeuT43LungGmGRSd");

        // 获取指定文件的输入流
        // File file = new File(filePath);
        // InputStream content = new FileInputStream(file);

        // 创建上传Object的Metadata
        ObjectMetadata meta = new ObjectMetadata();

        // 必须设置ContentLength
        meta.setContentLength(size);

        // 上传Object.
        PutObjectResult result = client.putObject("icake", "tags/" + date
                + "/" + filename, content, meta);

        // 打印ETag
        System.out.println(result.getETag());
    }
}
